package com.chj.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.chj.dao.OrderDAO;
import com.chj.dao.ParkDAO;
import com.chj.entity.Park;
import com.chj.entity.ParkModel;
import com.chj.service.ParkService;
import com.chj.util.DateUtil;
import com.chj.util.GenerateSequenceUtil;

@Service
public class ParkServiceImpl implements ParkService {

    @Resource
    private ParkDAO parkDAO;

    @Resource
    private OrderDAO orderDAO;

    @Override
    public List<Park> getParkListByNetWorkId(String netWorkId) {

        return parkDAO.getParkListByNetWorkId(netWorkId);
    }

    @Override
    public int saveParkWithNetWorkId(String netWorkId, Park park) {

        park.setParkId(GenerateSequenceUtil.getUUID());

        return parkDAO.save(park);
    }

    @Override
    public List<Park> getParkListByNetWorkId(String netWorkId, int page, int rows) {

        return parkDAO.getParkListByNetWorkId(netWorkId, page, rows);
    }

	@Override
	public List<ParkModel> getParkListAndStatus(String networkId, Map<String, Object> queryMap) {
		
		List<ParkModel> parkModelList = new ArrayList<ParkModel>();

		String  chargeTimeBegin = MapUtils.getString(queryMap  ,"chargeTimeBegin");
		String  chargeTimeEnd   = MapUtils.getString(queryMap  ,"chargeTimeEnd");
		
		List<Park> parkList = parkDAO.getParkListByNetWorkId(networkId);

		
		for (Park park : parkList) {

			ParkModel parkModel = new ParkModel();
			
			boolean parkStatus = orderDAO.queryParkStatus(networkId, park.getParkId(), chargeTimeBegin, chargeTimeEnd);
				
			if (parkStatus) {
				Date queryParkWaitTime = orderDAO.queryParkWaitTime(networkId, park.getParkId(), chargeTimeBegin, chargeTimeEnd);
				parkModel.setWaitingTime(DateUtil.dateToString(queryParkWaitTime,"yyyy-MM-dd hh:mm:ss"));
			} else {
				parkModel.setWaitingTime("无需等待");
			}

			parkModel.setPark(park);
			parkModel.setParkStatus(parkStatus);
			
			parkModelList.add(parkModel);
		}

		return parkModelList;
	}

    @Override
    public Park getParkById(String parkId) {
        
        return parkDAO.get(parkId);
    }

    @Override
    public int modifyPark(Park park) {

        return parkDAO.update(park);
    }

    @Override
    public int deletePark(Park park) {

        return parkDAO.delete(park);
    }
}
